<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增请假申请')" />
    <th:block th:include="include :: datetimepicker-css" />

    <link th:href="@{/ajax/libs/layui/css/layui.css}" rel="stylesheet"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-leave-add">
            <div class="form-group">
                <div class="col-sm-11">
                    <span style="margin-left: 30%">[[${holidayHours}]]</span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">请假类型：</label>
                <div class="col-sm-8">
                    <select name="type" id="type" class="form-control m-b" th:with="type=${@dict.getType('biz_leave_type')}">
                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                    </select>
                    <input name="title" id="title" class="form-control" type="text" style="display: none;">
                </div>
            </div>
            <div class="form-group" style="display: none">
                <label class="col-sm-3 control-label">部门：</label>
                <div class="col-sm-8">
                    <select name="deptId" class="form-control m-b" th:with="deptAll=${@deptService.getDeptAll()}" disabled="disabled">
                        <option value="">请选择</option>
                        <option th:each="dept : ${deptAll}" th:text="${dept.getShowName()}" th:value="${dept.getDeptId()}" th:selected="${dept.getDeptId() == hrEmp?.deptId}" th:hidden="${dept.getStatus() == '1' || dept.getDelFlag() != '0' }"></option>
                    </select>
                    <!--<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 代码生成请选择字典属性</span>-->
                </div>
            </div>
            <div class="form-group" style="display: none">
                <label class="col-sm-3 control-label">岗位：</label>
                <div class="col-sm-8">
                    <select name="postId" class="form-control m-b" th:with="postAll=${@postService.getPostAll()}" disabled="disabled">
                        <option value="">请选择</option>
                        <option th:each="post : ${postAll}" th:text="${post.getPostName()}" th:value="${post.getPostId()}"  th:selected="${post.getPostId() == hrEmp?.postId}" th:hidden="${post.getStatus() == '1'}"></option>
                    </select>
                    <!--<span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 代码生成请选择字典属性</span>-->
                </div>
            </div>
            <!--<div class="form-group">
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>标题：</label>
                <div class="col-sm-8">
                    <input name="title" class="form-control" type="text" required>
                </div>
            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>原因：</label>
                <div class="col-sm-8">
                    <textarea name="reason" class="form-control" required></textarea>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label"><span style="color: #ff0000; ">*</span>开始时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="startTime" class="form-control calcTotalSecondInput" readonly="readonly" placeholder="yyyy-MM-dd HH:mm:ss" type="text" required>
                    </div>
                </div>
            </div>
            <!--<div class="form-group">
                <div class="col-sm-3"></div>
                <div class="col-sm-8">
                <div class="radio check-box">
                    <label>
                        <input type="radio" checked="" value="0" name="startHalf" class="form-control leaveHalf" > <i></i> 上午</label>
                </div>
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="1" name="startHalf" class="form-control leaveHalf"> <i></i> 下午</label>
                </div>
                </div>
            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label"><span style="color: red; ">*</span>结束时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        <input name="endTime" class="form-control calcTotalSecondInput" placeholder="yyyy-MM-dd HH:mm:ss" readonly="readonly" type="text" required>
                    </div>
                </div>
            </div>
            <!--<div class="form-group">
                <div class="col-sm-3"></div>
                <div class="col-sm-8">
                <div class="radio check-box">
                    <label>
                        <input type="radio" value="0" name="endHalf" class="form-control leaveHalf"> <i></i> 上午</label>
                </div>
                <div class="radio check-box leaveHalf1" >
                    <label>
                        <input type="radio" checked="" value="1" name="endHalf" class="leaveHalf" > <i></i> 下午</label>
                </div>
                </div>
            </div>-->
            <div class="form-group">
                <label class="col-sm-3 control-label">请假时长：</label>
                <div class="col-sm-8">
                    <input name="totalTime" class="form-control" type="text" style="display: none;">
                    <input name="totalHours" class="form-control" type="text" style="display: none;">
                    <input name="hoursFlag" class="form-control" type="text" value="0" style="display: none;">
                    <input name="totalTimeText" class="form-control" type="text" readonly>
                </div>
            </div>
            <div class="form-group">
                <input id="imgUrls" name="imgUrls" type="hidden" >
                <label class="col-sm-3 control-label">相关证明：</label>
                <div class="col-sm-8">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn layui-btn-normal" id="testList">选择多图片</button>
                        <div class="layui-upload-list">
                            <table class="layui-table">
                                <thead>
                                <tr><th>文件名</th>
                                    <th>大小</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr></thead>
                                <tbody id="demoList"></tbody>
                            </table>
                        </div>
                        <button type="button" style="display: none;" class="layui-btn" id="testListAction" >开始上传</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />

    <script th:src="@{/ajax/libs/layui/layui.all.js}"></script>
    <script th:inline="javascript">
        var prefix = ctx + "hr/leave"
        var datas = [[${@dict.getType('biz_leave_type')}]];
        $("#form-leave-add").validate({
            focusCleanup: true
        });

        function submitHandler() {

            if ($.validate.form() && validateStartTimeAndEndTime()) {
                $('input[name="title"]').val($('#type option:selected').text());
                $("select[name=deptId]").attr("disabled",false);
                $("select[name=postId]").attr("disabled",false);

                debugger

                if ( $("input[name='hoursFlag']").val() == "0"){
                    $.modal.alertWarning("余额不足！");
                    return;
                }


                //上传图片
                $("#testListAction").click();

                if (!$("#demoList").html()){
                    //没有图片直接提交表单
                    $.operate.save(prefix + "/add", $('#form-leave-add').serialize());
                }

                // $.operate.save(prefix + "/add", $('#form-leave-add').serialize());
            }
        }

        $("input[name='startTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            minView: 0,//打开时显示的视图。0-'hour' 1-'day' 2-'month' 3-'year' 4-'decade'
            autoclose: true,
            minuteStep: 30
        });

        $("input[name='endTime']").datetimepicker({
            format: "yyyy-mm-dd hh:ii",
            minView: 0,//打开时显示的视图。0-'hour' 1-'day' 2-'month' 3-'year' 4-'decade'
            autoclose: true,
            minuteStep: 30
        });


        // 监听开始和结束日期填写，动态生成请假时长
        $(".calcTotalSecondInput").change(function () {

            $.ajax({
                type: 'POST',
                url: prefix + "/leaveTime",
                data: $('#form-leave-add').serialize(),
                dataType: 'json',
                success: function (result) {
                        if (result.msg == "success") {
                            // console.log(result.data);
                            console.log(result.data["hours"])
                            $('input[name="totalTimeText"]').val(result.data["hours"] + " 小时");
                            $('input[name="totalTime"]').val(result.data["days"]);
                            $('input[name="totalHours"]').val(result.data["hours"]);
                            //余额标识
                            $('input[name="hoursFlag"]').val("1");
                        } else {
                            console.log(result.data)
                            $('input[name="totalTimeText"]').val(result.data["hours"] + " 小时");
                            //余额标识
                            $('input[name="hoursFlag"]').val("0");
                            $.modal.alertWarning(result.msg);
                        }
                },
                error: function (error) {
                    $.modal.alertWarning("系统错误，请联系管理员");
                }

            });

        });

        function validateStartTimeAndEndTime() {
            if ($('input[name="startTime"]').val() > $('input[name="endTime"]').val()) {
                $.modal.alertWarning("结束时间必须大于开始时间");
                return false;
            }
            return true;
        }

        $(document).ready(function () {

            layui.use('upload', function(){
                var $ = layui.jquery
                    ,upload = layui.upload;

                //多文件列表示例
                var demoListView = $('#demoList')
                    , uploadListIns = upload.render({
                    elem: '#testList'
                    , url: '/common/upload' //改成您自己的上传接口
                    , accept: 'images'
                    , multiple: true
                    , acceptMime:  'image/*'
                    , auto: false
                    , bindAction: '#testListAction'
                    , choose: function (obj) {
                        debugger
                        var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列
                        //读取本地文件
                        obj.preview(function (index, file, result) {
                            var tr = $(['<tr id="upload-' + index + '">'
                                /*, '<td>' + file.name + '</td>'*/
                                , '<td>' +'<img onclick="$.table.imageViewSimp(\''+ result +'\')" src="'+ result +'">' + '</td>'
                                , '<td>' + (file.size / 1024).toFixed(1) + 'kb</td>'
                                , '<td>等待上传</td>'
                                , '<td>'
                                , '<button class="layui-btn layui-btn-xs demo-reload layui-hide">重传</button>'
                                , '<button class="layui-btn layui-btn-xs layui-btn-danger demo-delete">删除</button>'
                                , '</td>'
                                , '</tr>'].join(''));

                            //单个重传
                            tr.find('.demo-reload').on('click', function () {
                                obj.upload(index, file);
                            });

                            //删除
                            tr.find('.demo-delete').on('click', function () {
                                delete files[index]; //删除对应的文件
                                tr.remove();
                                uploadListIns.config.elem.next()[0].value = ''; //清空 input file 值，以免删除后出现同名文件不可选
                            });

                            demoListView.append(tr);
                        });
                    }
                    , done: function (res, index, upload) {
                        console.log("done:"+res); //得到总文件数
                        console.log("done:"+index); //请求成功的文件数
                        console.log("done:"+upload); //请求失败的文件数
                        if (res.code == 0) { //上传成功

                            var tr = demoListView.find('tr#upload-' + index)
                                , tds = tr.children();
                            tds.eq(2).html('<span style="color: #5FB878;">上传成功</span>');
                            tds.eq(3).html(''); //清空操作

                            $('#imgUrls').val($('#imgUrls').val() + "," + res.url);

                            return delete this.files[index]; //删除文件队列已经上传成功的文件
                        }
                        this.error(index, upload);
                    },allDone: function(obj){ //当文件全部被提交后，才触发
                        console.log("allDone:"+obj.total); //得到总文件数
                        console.log("allDone:"+obj.successful); //请求成功的文件数
                        console.log("allDone:"+obj.aborted); //请求失败的文件数

                        //提交表单
                        $.operate.save(prefix + "/add", $('#form-leave-add').serialize());
                    }
                    , error: function (index, upload) {
                        var tr = demoListView.find('tr#upload-' + index)
                            , tds = tr.children();
                        tds.eq(2).html('<span style="color: #FF5722;">上传失败</span>');
                        tds.eq(3).find('.demo-reload').removeClass('layui-hide'); //显示重传
                    }
                });
            });
        });




    </script>
</body>
</html>
